<?php

/* ----------------------------------------------
  Caloris: Administration Store Extensions Browser

  $Id$
  $Author$

  Prohlížeč extenzí ke stažení
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'store');

$ch = curl_init(); // create cURL handle (ch)

if (!$ch) {
    die("Couldn't initialize");
}

// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, 'http://caloris.cz/store/extensions/' . $_GET["file"] . '.txt');
$ret = curl_setopt($ch, CURLOPT_HEADER, false);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_setopt($ch, CURLOPT_NOBODY, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 40);
// execute
$ret = curl_exec($ch);

if (empty($ret)) {
    // some kind of an error happened
    die(curl_error($ch));
    curl_close($ch); // close cURL handler
} else {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler

    if (empty($info['http_code'])) {
        die("No HTTP code was returned");
    } else {
        $xma = $ret;
    }
}

$content = '
<h1>' . $_GET["file"] . '</h1>

<div style="border: 1px solid #e5e8ff; padding: 2px; overflow: hidden; width: 95%; margin-top: 10px;">
<div style="background-color: #F3F5FF; padding: 10px;">
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/store/store.extensions.php" method="post">
<p><textarea style="width: 100%; height: 300px;" disabled="disabled">' . $xma . '</textarea></p>' . PHP_EOL;

if (!file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_EXTENSION . '/' . $_GET["file"] . '.php')) {
    $content .= '
<p style="margin-top: 10px;"><input type="hidden" name="function_name" value="store_extension_install" />
<input type="hidden" name="transport_type" value="form" />
<input type="hidden" name="checksum" value="' . $_GET["checksum"] . '" />
<input type="hidden" name="file" value="' . $_GET["file"] . '" />
<input type="submit" value="Nainstalovat" /></p>';
} else {
    $content .= '
<p style="margin-top: 10px;">';
}

$content .= '
</form>
</div>
</div>' . PHP_EOL;

$data["title"] = 'Store: Extenze: Prohlížeč';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');